package com.user.seat.dao.impl;

import com.user.seat.dao.SeatDao;
import com.user.seat.domain.*;
import com.user.seat.mapper.SeatNumberMapper;
import com.user.seat.mapper.SeatStatusMapper;
import com.user.seat.utils.JwtService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author jimmy
 * Date  2022/4/22 14:28
 * @version 1.0
 */
@Service
public class SeatDaoImpl implements SeatDao {

    private final SeatStatusMapper seatStatusMapper;
    private final SeatNumberMapper seatNumberMapper;
    private final JwtService jwtService;
    public SeatDaoImpl(SeatStatusMapper seatStatusMapper,
                       SeatNumberMapper seatNumberMapper,
                       JwtService jwtService){
        this.seatStatusMapper = seatStatusMapper;
        this.seatNumberMapper = seatNumberMapper;
        this.jwtService = jwtService;
    }

    @Override
    public void setSeatsNumber(SeatNumber seatNumber) {
        Date date = new Date();
        seatNumber.setCreatedTime(date);

        SeatNumberExample example = new SeatNumberExample();
        SeatNumberExample.Criteria criteria = example.createCriteria();
        criteria.andSeatTypeEqualTo("seat");
        long count = seatNumberMapper.countByExample(example);
        if (count>0){
            SeatNumberExample updateExample = new SeatNumberExample();
            SeatNumberExample.Criteria updateCriteria = example.createCriteria();
            updateCriteria.andSeatTypeEqualTo("seat");
            seatNumber.setUpdatedTime(date);
            seatNumberMapper.updateByExampleSelective(seatNumber,updateExample);
        }else {
            seatNumber.setCreatedTime(date);
            seatNumber.setUpdatedTime(date);
            seatNumber.setSeatType("seat");
            seatNumberMapper.insert(seatNumber);
        }
    }

    @Override
    public int getSeatsNumber() {
        SeatNumberExample example = new SeatNumberExample();
        SeatNumberExample.Criteria criteria = example.createCriteria();
        criteria.andSeatTypeEqualTo("seat");
        List<SeatNumber> seatNumberList = seatNumberMapper.selectByExample(example);
        if (seatNumberList.size()>0){
            return seatNumberList.stream().findFirst().get().getSeatNumber();
        }else {
            return 15;
        }
    }

    @Override
    public int insertSeat(Integer number, SeatStatus seatStatus) {
        SeatStatusExample example = new SeatStatusExample();
        SeatStatusExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(seatStatus.getUserId());
        criteria.andStatusEqualTo("0");
        criteria.andSeatNumberEqualTo(number);
        int id = 0;
        List<SeatStatus> seatStatusList = seatStatusMapper.selectByExample(example);
        if (seatStatusList.size()<1){
            Date date = new Date();
            seatStatus.setStatus("0");
            seatStatus.setCreatedTime(date);
            seatStatus.setUpdatedTime(date);
            seatStatus.setSeatNumber(number);
            id = seatStatusMapper.insert(seatStatus);
        }
        return id;
    }

    @Override
    public List<SeatStatus> getSeat() {
        SeatStatusExample example = new SeatStatusExample();
        SeatStatusExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("0");
        return seatStatusMapper.selectByExample(example);
    }

    @Override
    public int updateSeatByStatus() {
        SeatStatus seatStatus = new SeatStatus();
        SeatStatusExample example = new SeatStatusExample();
        SeatStatusExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("1");
        return seatStatusMapper.updateByExampleSelective(seatStatus,example);
    }

    @Override
    public int updateSeatByUserId(SeatStatus seatStatus) {
        String admin = seatStatus.getAdmin();
        if(!jwtService.verifyIsAdmin(admin)){
            return 0;
        }
        SeatStatusExample example = new SeatStatusExample();
        SeatStatusExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(seatStatus.getUserId());
        criteria.andSeatNumberEqualTo(seatStatus.getSeatNumber());
        seatStatus.setStatus("1");
        return seatStatusMapper.updateByExampleSelective(seatStatus,example);
    }

    @Override
    public int doResetSeat() {
        SeatStatusExample example = new SeatStatusExample();
        SeatStatusExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("0");
        SeatStatus seatStatus = new SeatStatus();
        return seatStatusMapper.updateByExampleSelective(seatStatus,example);
    }
}
